package com.skydroid.fpvplayerdemo;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Size;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author 咔一下
 * @date 2021-5-6 11:39
 * @email 1501020210@qq.com
 */
public class PermissionManager{

    private static PermissionManager instance = null;

    public synchronized static PermissionManager getInstance() {
        if (instance == null){
            instance = new PermissionManager();
        }
        return instance;
    }

    private final ConcurrentHashMap<Integer, PermissionsResultCallback> PermissionRequesCodeMap = new ConcurrentHashMap<Integer,PermissionsResultCallback>();

    private PermissionManager(){}

    public void checkPermissions(Activity activity,int requestCode, PermissionsResultCallback result, String... permissions){
        if (hasPermissions(activity,permissions)){
            result.onGranted();
            return;
        }
        if (PermissionRequesCodeMap.containsKey(requestCode)){
            if (result != null){
                result.onError(new IllegalArgumentException("requestCode已存在"));
            }
            return;
        }
        ActivityCompat.requestPermissions(activity,permissions,requestCode);
//        EasyPermissions.requestPermissions(activity,remarks,requestCode,permissions);
        PermissionRequesCodeMap.put(requestCode,result);
    }

    /**
     * 由外部Activitys手动调用
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionsResultCallback callback = PermissionRequesCodeMap.remove(requestCode);
//        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults);
        if (callback == null){
            return;
        }
        List<String> denied = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            String perm = permissions[i];
            if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                denied.add(perm);
            }
        }
        // Report denied permissions, if any.
        if (!denied.isEmpty()) {
            callback.onDenied(denied.toArray(new String[0]));
        }else {
            callback.onGranted();
        }
    }

    public static boolean hasPermissions(@NonNull Context context,
                                         @Size(min = 1) @NonNull String... perms) {
        // Always return true for SDK < M, let the system deal with the permissions
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Log.w("PermissionManager", "hasPermissions: API version < M, returning true by default");

            // DANGER ZONE!!! Changing this will break the library.
            return true;
        }

        // Null context may be passed if we have detected Low API (less than M) so getting
        // to this point with a null context should not be possible.
        if (context == null) {
            throw new IllegalArgumentException("Can't check permissions for null context");
        }

        for (String perm : perms) {
            if (ContextCompat.checkSelfPermission(context, perm)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }

        return true;
    }

}